package com.sjgame.quartz;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;

/**
 * @program: shgame
 * @description:
 * @author: zcl
 * @create: 2018-06-28 19:39
 **/

@Configuration
public class MySchedulerListener implements ApplicationListener<ContextRefreshedEvent> {




    @Autowired
    MyScheduler myScheduler;

    @Autowired
    SpringJobFactory springJobFactory;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
//        try {
//            myScheduler.schedulerJob();
//            System.out.println("SynchronizedData job  start...");
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
    }

    @Bean(name ="mySchedulerFactoryBean")
    public SchedulerFactoryBean mySchedulerFactory() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setOverwriteExistingJobs(true);
        bean.setStartupDelay(1);
        bean.setJobFactory(springJobFactory);
        return bean;
    }

    @Bean(name="Scheduler")
    public Scheduler scheduler() throws IOException {
        return mySchedulerFactory().getScheduler();
    }

}